package com.base.weeks.weekof271th;

/**
 * TODO
 *
 * @author leon
 * @date 2021年12月12日 11:40
 */
public class MinimumRefill {

    public int minimumRefill(int[] plants, int capacityA, int capacityB) {
        int res = 0;
        int n = plants.length;
        int i = 0;
        int j = n - 1;
        int A = capacityA,B = capacityB;
        for (; i < j; i++, j--) {
            if(A>=plants[i]){
                A -= plants[i];
            }else {
                A = capacityA - plants[i];
                res++;
            }

            if(B>=plants[j]){
                B -= plants[j];
            }else {
                B = capacityB-plants[j];
                res++;
            }
        }
        // 只有b大于a才给b
        if(i == j){
            if(A<B){
                if(B<plants[i]){
                    return ++res;
                }
            }else {
                if(A<plants[i]){
                    return ++res;
                }
            }
        }

        return res;
    }

    public static void main(String[] args) {
        System.out.println(new MinimumRefill().minimumRefill(new int[]{1,2,4,5},6,5));
    }
}
